package cn.szzsi._admin;

import cn.szzsi.common.kit.TemplateConfigUtil;
import cn.szzsi.service.HtmlService;
import cn.szzsi.common.bean.HtmlConfig;
import cn.szzsi.common.kit.CommonUtil;

import cn.szzsi.common.model.*;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 商户后台类 - 文章
 */
public class ArticleController extends BaseAdminController<Article> {
    private Article article;
    private String path;//模板路径
    private String shopId;;//商户id,也是用户的店铺id
    // 列表
    public void list(){
        shopId=getAttr("shopId");
        findBusinessByPage(shopId,"mall_article");
        setAttr("shopId",shopId);
        render("articles_list.html");
    }
    //ajax list
    public void ajaxlist(){
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        List<Article> lpa = Article.dao.find("select * from mall_article where uid = ? order by createDate desc",shopId);
        for(Article pa:lpa){
            pa.init();
        }
        renderJson(lpa);
    }

    /**
     * 文章的列表页面
     */
    @Clear
    public void waplist(){
        shopId=getAttr("shopId");
        path=CommonUtil.getPathByUidAndType(shopId,CommonUtil.isMobileDevice(getRequest()));

        id = getPara("id","");
        ArticleCategory ac = ArticleCategory.dao.findById(id);
        if(ac != null){
            Page<Article> pa = Article.dao.getArticlePager(1,20,ac);
            setAttr("pa",pa);
        }
        setAttr("base", getRequest().getContextPath());
        setAttr("ac",ac);
        setAttr("shopId", shopId);

        render(CommonUtil.getRealTemplatePath(path,"article_wap_list.html"));
    }

    /**
     * 文章的详情页面
     */
    @Clear
    public void wap(){
        shopId=getAttr("shopId");
        path=CommonUtil.getPathByUidAndType(shopId,CommonUtil.isMobileDevice(getRequest()));

        id = getPara("id","");
        Article a = Article.dao.findById(id);
        setAttr("a",a);
        setAttr("base", getRequest().getContextPath());

        render(CommonUtil.getRealTemplatePath(path,"article_wap_detail.html"));
    }

    // 添加
    public void add() {
        shopId=getAttr("shopId");
        setAttr("shopId",shopId);
        setAttr("articleCategoryTreeList", ArticleCategory.dao.getByUid(shopId));
        render("articles_input.html");
    }

    // 编辑
    public void edit() {
        shopId=getAttr("shopId");
        String id = getPara("id","");
        if(StrKit.notBlank(id)){
            setAttr("article", Article.dao.findById(id));
        }
        setAttr("shopId",shopId);
        setAttr("articleCategoryTreeList", ArticleCategory.dao.getByUid(shopId));
        render("articles_input.html");
    }

    // 保存
    public void save(){
        shopId=getAttr("shopId");
        article = getModel(Article.class);
        article.set("uid",shopId);
        article.set("hits", 0);
        article.set("pageCount", 0);
        HtmlConfig htmlConfig = TemplateConfigUtil.getBusinessHtmlConfig(HtmlConfig.ARTICLE_CONTENT);
        String htmlFilePath = htmlConfig.getBusinessHtmlFilePath();
        String shopAlias= SiteConfig.getByShopId(shopId).getStr("shopAlias");//获取店铺别名
        article.set("htmlFilePath", "/"+shopAlias+htmlFilePath);
        saved(article);

        //生成文章静态模板
        boolean isMobileDevice= CommonUtil.isMobileDevice(getRequest());//判断是pc访问还是wap访问
        String path=CommonUtil.getPathByUidAndType(shopId,isMobileDevice);//获取模板路径
        setAttr(CommonUtil.BUSINESS_TEMPLATE_PATH,path);
        HtmlService.service.businessArticleContentBuildHtml(article,isMobileDevice,path,shopId,shopAlias);
        renderJson(Ret.ok("info","添加成功").set("url","list"));
        //redirect("/shop/articles/list");
    }

    // 更新
    public void update(){
        shopId=getAttr("shopId");
        article = getModel(Article.class);
        //删除文章静态路径
        /*List<String> htmlFilePathList = article.getHtmlFilePathList();
        if (htmlFilePathList != null && htmlFilePathList.size() > 0) {
            for (String htmlFilePath : htmlFilePathList) {
                File htmlFile = new File(PathKit.getWebRootPath() + htmlFilePath);
                if (htmlFile.exists()) {
                    htmlFile.delete();
                }
            }
        }*/
        updated(article);

        //生成文章静态模板
        boolean isMobileDevice= CommonUtil.isMobileDevice(getRequest());//判断是pc访问还是wap访问
        String path=CommonUtil.getPathByUidAndType(shopId,isMobileDevice);//获取模板路径
        setAttr(CommonUtil.BUSINESS_TEMPLATE_PATH,path);
        String shopAlias= SiteConfig.getByShopId(shopId).getStr("shopAlias");//获取店铺别名
        HtmlService.service.businessArticleContentBuildHtml(article,isMobileDevice,path,shopId,shopAlias);

        renderJson(Ret.ok("info","修改成功").set("url","list"));
        ///redirect("/shop/articles/list");
    }

    // 删除
    public void delete(){
        String id=getPara("id");
        if(Article.dao.deleteById(id)){
            renderJson(Ret.ok());
        }else{
            renderJson(Ret.fail());

        }
    }

    public static void main(String[] args) {
        System.out.println(Ret.ok());
    }

    //购物须知
    @Clear
    public void help(){
        shopId=getAttr("shopId");
        String shopAlias= SiteConfig.getByShopId(shopId).getStr("shopAlias");//获取店铺别名
        path=CommonUtil.getPathByUidAndType(shopId,CommonUtil.isMobileDevice(getRequest()));
        Article help= Article.dao.getHelp();
        setAttr("help",help);
        setAttr("base", getRequest().getContextPath());
        setAttr("shopId",shopId);
        setAttr("shopAlias",shopAlias);
        setAttr("path",path);
        System.out.println(CommonUtil.getRealTemplatePath(path,"help.html"));
        render(CommonUtil.getRealTemplatePath(path,"help.html"));
    }

//    ----------个性化功能分割线-----------

    /**
     * 招商加盟
     */
    @Clear
    public void zsjm(){
        shopId=getAttr("shopId");
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);

        List<Admanage> businessAdImgList= Admanage.dao.getAdImageByUid(shopId);
        setAttr("businessAdImgList",businessAdImgList);
        path=CommonUtil.getPathByUidAndType(shopId,CommonUtil.isMobileDevice(getRequest()));//获取模板路径
        setAttr("shopId",shopId);
        setAttr("path",path);
        setAttr("siteConfig",siteConfig);
        setAttr("base", getRequest().getContextPath());
        setAttr("shopAlias",siteConfig.getStr("shopAlias"));
        if(!CommonUtil.PLATFORM_MEMBER.equals(shopId) && !CommonUtil.PLATFORM_BUSINESS.equals(shopId)){
            render(CommonUtil.getRealTemplatePath(path,"zsjm.html"));
        }else{
            System.out.println("**************非用户操作");
        }
    }
}
